﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RATSharp.Model;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Runtime.Serialization;

namespace RATSharpW7.ViewModel
{
    /// <summary>
    /// Basic reservation ModelView. Usable for all view where some sort
    /// of list period must be shown to the user
    /// </summary>
    [DataContract]
    public class ReservationViewModel : CollectionViewModel<period, List<period>>
      {
        protected void notifyChangeModel(Task<Reservations> task)
        {
            task.ContinueWith(t =>
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    Exception exception = t.Exception;

                    if (t.Result != null)
                    {
                        extractReservations(t.Result);
                    }
                });
            });
        }

        /// <summary>
        /// defines how to modify Reservations collection for View 
        /// </summary>
        /// <param name="resList"></param>
        protected virtual void extractReservations(Reservations resList)
        {
            //quite useful to link all periods with reservation
            ReservationFactory.MakeBidirectionalConnection(resList);
            this.loadedObjectsDataSource.Clear();
            foreach (Reservation res in resList)
            {
                foreach (period per in res.periods)
                {
                    this.loadedObjectsDataSource.Add(per);
                }
            }
        }

    }
}
